package com.slkj.shilixiaoyuanapp.view.dialog;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.entity.MainShowZsEntity;
import com.slkj.shilixiaoyuanapp.ui.main.MainActivity;
import com.slkj.shilixiaoyuanapp.view.base.BaseBottomDialog;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created  on 2018-08-09
 *
 * @author 唐开阔
 * @describe
 */
public class ShareDialog extends BaseBottomDialog {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recycer)
    RecyclerView recycer;
    private String url;
    List<Data> data = new ArrayList<>();
    BaseQuickAdapter adapter;
    UMWeb umWeb;
    public void setUrl(String url,String title,String content) {
        umWeb =   new UMWeb(url);
        umWeb.setTitle(title);//标题
        //umWeb.setThumb(new UMImage(getActivity(),img));  //缩略图
        umWeb.setDescription(content);//描述

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        data.add(new Data("微信", R.drawable.ic_share_wx));
        data.add(new Data("朋友圈", R.drawable.ic_share_pyq));
        data.add(new Data("QQ", R.drawable.ic_share_qq));
        data.add(new Data("QQ空间", R.drawable.ic_share_qqkj));
        adapter = new BaseQuickAdapter<Data, BaseViewHolder>(R.layout.item_share, data) {
            @Override
            protected void convert(BaseViewHolder helper, final Data item) {
                helper.setText(R.id.tv_name, item.getName()).setImageResource(R.id.img, item.getRes()).itemView.setOnClickListener(v -> {
                    switch (helper.getAdapterPosition()) {
                        case 0:
                            new ShareAction(getActivity()).setPlatform(SHARE_MEDIA.WEIXIN)//传入平台
                                    .withMedia(umWeb).share();
                            break;
                        case 1:
                            new ShareAction(getActivity()).setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)
                                    .withMedia(umWeb).share();
                            break;
                        case 2:
                            new ShareAction(getActivity()).setPlatform(SHARE_MEDIA.QQ)
                                    .withMedia(umWeb).share();
                            break;
                        case 3:
                            new ShareAction(getActivity()).setPlatform(SHARE_MEDIA.QZONE)
                                    .withMedia(umWeb).share();
                            break;
                    }
                });

            }
        };
    }

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_share;
    }

    @OnClick(R.id.tv_qx)
    void disimiss() {
        dismiss();
    }

    @Override
    public void bindView() {
        recycer.setLayoutManager(new GridLayoutManager(getContext(), 4));
        recycer.setAdapter(adapter);

    }

    @Override
    public int getHeight() {
        return (int) (getActivity().getWindowManager().getDefaultDisplay().getWidth() / 2);
    }

    static class Data {
        String name;
        int res;

        public Data(String name, int res) {
            this.name = name;
            this.res = res;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getRes() {
            return res;
        }

        public void setRes(int res) {
            this.res = res;
        }
    }
}
